export default class VideoUtil{
	public static getCover(url: string, frame?: number):Promise<string> { //封面
		return new Promise((resolve) => {
			let video = document.createElement("video");
			video.setAttribute('crossOrigin', 'anonymous'); //处理跨域
			video.setAttribute('src', url);
			video.setAttribute('width', '400px');
			video.setAttribute('height', '240px');
			video.currentTime = frame || 1; // 这里是截取第几帧
			console.log(video)
			video.addEventListener('loadeddata', function() { //创建虚拟视频组件拿第一帧
				let canvas = document.createElement("canvas")
				let width = video.width //canvas的尺寸和图片一样
				let height = video.height
				canvas.width = width;
				canvas.height = height;
				canvas.getContext("2d").drawImage(video, 0, 0, width, height); //绘制canvas
				console.log(canvas.toDataURL('image/jpeg'))
				resolve(canvas.toDataURL('image/jpeg'))
			});
		})
	}
	
	public static changimg(val) { //base64转图片
		// Base64编码的字符串
		var base64Str = val
		// 创建一个Image对象
		var img = new Image();
		// 设置Image的src为Base64编码的字符串
		img.src = base64Str;
		// 将Image对象绘制到Canvas上
		var canvas = document.createElement('canvas');
		canvas.width = img.width;
		canvas.height = img.height;
		var ctx = canvas.getContext('2d');
		ctx.drawImage(img, 0, 0);
		// 将Canvas转换为DataURL
		var dataURL = canvas.toDataURL();
		return dataURL
	}
}